<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>模型类序列化器ModelSerializer | 课程介绍</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C04-View/index.html" />
    
    
    <link rel="prev" href="../C03-Serializer/Deserializing.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.4"
        data-chapter-title="模型类序列化器ModelSerializer"
        data-filepath="C03-Serializer/ModelSerializer.md"
        data-basepath=".."
        data-revision="Thu May 10 2018 07:57:11 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-IntroduceToDRF/index.html">
            
                
                    <a href="../C01-IntroduceToDRF/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        引入DjangoRESTframework
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
            
                
                    <a href="../C01-IntroduceToDRF/SeparatedFrontendAndBackend.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Web应用模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-IntroduceToDRF/IntroduceToREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/IntroduceToREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        认识RESTful
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-IntroduceToDRF/HowToDesignREST.html">
            
                
                    <a href="../C01-IntroduceToDRF/HowToDesignREST.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        RESTful设计方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
            
                
                    <a href="../C01-IntroduceToDRF/DevelopRESTAPIWithDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        使用Django开发REST接口
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
            
                
                    <a href="../C01-IntroduceToDRF/CoreTaskToDevelopRESTAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        明确REST接口开发的核心任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-IntroduceToDRF/AboutDRF.html">
            
                
                    <a href="../C01-IntroduceToDRF/AboutDRF.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        DjangoRESTframework简介
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-DRFProject/index.html">
            
                
                    <a href="../C02-DRFProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        DRF工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-DRFProject/InstallAndConfig.html">
            
                
                    <a href="../C02-DRFProject/InstallAndConfig.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装与配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-DRFProject/TheFirstDRFProgram.html">
            
                
                    <a href="../C02-DRFProject/TheFirstDRFProgram.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        见识DRF的魅力
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Serializer/index.html">
            
                
                    <a href="../C03-Serializer/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Serializer序列化器
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Serializer/Declaring.html">
            
                
                    <a href="../C03-Serializer/Declaring.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        定义Serializer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Serializer/Serializing.html">
            
                
                    <a href="../C03-Serializer/Serializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Serializer/Deserializing.html">
            
                
                    <a href="../C03-Serializer/Deserializing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        反序列化使用
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.4" data-path="C03-Serializer/ModelSerializer.html">
            
                
                    <a href="../C03-Serializer/ModelSerializer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        模型类序列化器ModelSerializer
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-View/index.html">
            
                
                    <a href="../C04-View/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-View/RequestAndResponse.html">
            
                
                    <a href="../C04-View/RequestAndResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Request与Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-View/View.html">
            
                
                    <a href="../C04-View/View.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        视图概览
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-View/ViewIntroduction.html">
            
                
                    <a href="../C04-View/ViewIntroduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        视图说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-View/ViewSet.html">
            
                
                    <a href="../C04-View/ViewSet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        视图集ViewSet
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-View/Routers.html">
            
                
                    <a href="../C04-View/Routers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        路由Router
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Components/index.html">
            
                
                    <a href="../C05-Components/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        其他功能
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Components/Authentication.html">
            
                
                    <a href="../C05-Components/Authentication.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        认证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Components/Permissions.html">
            
                
                    <a href="../C05-Components/Permissions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        权限
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C05-Components/Throttling.html">
            
                
                    <a href="../C05-Components/Throttling.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        限流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Components/Filtering.html">
            
                
                    <a href="../C05-Components/Filtering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        过滤
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="C05-Components/Pagination.html">
            
                
                    <a href="../C05-Components/Pagination.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="C05-Components/Versioning.html">
            
                
                    <a href="../C05-Components/Versioning.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        版本
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="C05-Components/Exceptions.html">
            
                
                    <a href="../C05-Components/Exceptions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="C05-Components/Documents.html">
            
                
                    <a href="../C05-Components/Documents.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        自动生成接口文档
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程介绍</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x6A21;&#x578B;&#x7C7B;&#x5E8F;&#x5217;&#x5316;&#x5668;modelserializer">&#x6A21;&#x578B;&#x7C7B;&#x5E8F;&#x5217;&#x5316;&#x5668;ModelSerializer</h1>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x4F7F;&#x7528;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5BF9;&#x5E94;&#x7684;&#x662F;Django&#x7684;&#x6A21;&#x578B;&#x7C7B;&#xFF0C;DRF&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;ModelSerializer&#x6A21;&#x578B;&#x7C7B;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x6765;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x5FEB;&#x901F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;Serializer&#x7C7B;&#x3002;</p>
<p>ModelSerializer&#x4E0E;&#x5E38;&#x89C4;&#x7684;Serializer&#x76F8;&#x540C;&#xFF0C;&#x4F46;&#x63D0;&#x4F9B;&#x4E86;&#xFF1A;</p>
<ul>
<li>&#x57FA;&#x4E8E;&#x6A21;&#x578B;&#x7C7B;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x4E00;&#x7CFB;&#x5217;&#x5B57;&#x6BB5;</li>
<li>&#x57FA;&#x4E8E;&#x6A21;&#x578B;&#x7C7B;&#x81EA;&#x52A8;&#x4E3A;Serializer&#x751F;&#x6210;validators&#xFF0C;&#x6BD4;&#x5982;unique_together</li>
<li>&#x5305;&#x542B;&#x9ED8;&#x8BA4;&#x7684;create()&#x548C;update()&#x7684;&#x5B9E;&#x73B0;</li>
</ul>
<h2 id="1--&#x5B9A;&#x4E49;">1.  &#x5B9A;&#x4E49;</h2>
<p>&#x6BD4;&#x5982;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;BookInfoSerializer</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = <span class="hljs-string">&apos;__all__&apos;</span>
</code></pre>
<ul>
<li>model  &#x6307;&#x660E;&#x53C2;&#x7167;&#x54EA;&#x4E2A;&#x6A21;&#x578B;&#x7C7B;</li>
<li>fields  &#x6307;&#x660E;&#x4E3A;&#x6A21;&#x578B;&#x7C7B;&#x7684;&#x54EA;&#x4E9B;&#x5B57;&#x6BB5;&#x751F;&#x6210;</li>
</ul>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;python manage.py shell&#x4E2D;&#x67E5;&#x770B;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x7684;BookInfoSerializer&#x7684;&#x5177;&#x4F53;&#x5B9E;&#x73B0;</p>
<pre><code class="lang-python"><span class="hljs-prompt">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> booktest.serializers <span class="hljs-keyword">import</span> BookInfoSerializer
<span class="hljs-prompt">&gt;&gt;&gt; </span>serializer = BookInfoSerializer()
<span class="hljs-prompt">&gt;&gt;&gt; </span>serializer
BookInfoSerializer():
    id = IntegerField(label=<span class="hljs-string">&apos;ID&apos;</span>, read_only=<span class="hljs-keyword">True</span>)
    btitle = CharField(label=<span class="hljs-string">&apos;&#x540D;&#x79F0;&apos;</span>, max_length=<span class="hljs-number">20</span>)
    bpub_date = DateField(allow_null=<span class="hljs-keyword">True</span>, label=<span class="hljs-string">&apos;&#x53D1;&#x5E03;&#x65E5;&#x671F;&apos;</span>, required=<span class="hljs-keyword">False</span>)
    bread = IntegerField(label=<span class="hljs-string">&apos;&#x9605;&#x8BFB;&#x91CF;&apos;</span>, max_value=<span class="hljs-number">2147483647</span>, min_value=-<span class="hljs-number">2147483648</span>, required=<span class="hljs-keyword">False</span>)
    bcomment = IntegerField(label=<span class="hljs-string">&apos;&#x8BC4;&#x8BBA;&#x91CF;&apos;</span>, max_value=<span class="hljs-number">2147483647</span>, min_value=-<span class="hljs-number">2147483648</span>, required=<span class="hljs-keyword">False</span>)
    image = ImageField(allow_null=<span class="hljs-keyword">True</span>, label=<span class="hljs-string">&apos;&#x56FE;&#x7247;&apos;</span>, max_length=<span class="hljs-number">100</span>, required=<span class="hljs-keyword">False</span>)
</code></pre>
<h2 id="2--&#x6307;&#x5B9A;&#x5B57;&#x6BB5;">2.  &#x6307;&#x5B9A;&#x5B57;&#x6BB5;</h2>
<p>1) &#x4F7F;&#x7528;<strong>fields</strong>&#x6765;&#x660E;&#x786E;&#x5B57;&#x6BB5;&#xFF0C;<code>__all__</code>&#x8868;&#x540D;&#x5305;&#x542B;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5199;&#x660E;&#x5177;&#x4F53;&#x54EA;&#x4E9B;&#x5B57;&#x6BB5;&#xFF0C;&#x5982;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;btitle&apos;</span>, <span class="hljs-string">&apos;bpub_date&apos;</span>)
</code></pre>
<p>2) &#x4F7F;&#x7528;<strong>exclude</strong>&#x53EF;&#x4EE5;&#x660E;&#x786E;&#x6392;&#x9664;&#x6389;&#x54EA;&#x4E9B;&#x5B57;&#x6BB5;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        exclude = (<span class="hljs-string">&apos;image&apos;</span>,)
</code></pre>
<p>3) &#x9ED8;&#x8BA4;ModelSerializer&#x4F7F;&#x7528;&#x4E3B;&#x952E;&#x4F5C;&#x4E3A;&#x5173;&#x8054;&#x5B57;&#x6BB5;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>depth</strong>&#x6765;&#x7B80;&#x5355;&#x7684;&#x751F;&#x6210;&#x5D4C;&#x5957;&#x8868;&#x793A;&#xFF0C;depth&#x5E94;&#x8BE5;&#x662F;&#x6574;&#x6570;&#xFF0C;&#x8868;&#x660E;&#x5D4C;&#x5957;&#x7684;&#x5C42;&#x7EA7;&#x6570;&#x91CF;&#x3002;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroInfoSerializer2</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = HeroInfo
        fields = <span class="hljs-string">&apos;__all__&apos;</span>
        depth = <span class="hljs-number">1</span>
</code></pre>
<p>&#x5F62;&#x6210;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x5668;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python">HeroInfoSerializer():
    id = IntegerField(label=<span class="hljs-string">&apos;ID&apos;</span>, read_only=<span class="hljs-keyword">True</span>)
    hname = CharField(label=<span class="hljs-string">&apos;&#x540D;&#x79F0;&apos;</span>, max_length=<span class="hljs-number">20</span>)
    hgender = ChoiceField(choices=((<span class="hljs-number">0</span>, <span class="hljs-string">&apos;male&apos;</span>), (<span class="hljs-number">1</span>, <span class="hljs-string">&apos;female&apos;</span>)), label=<span class="hljs-string">&apos;&#x6027;&#x522B;&apos;</span>, required=<span class="hljs-keyword">False</span>, validators=[&lt;django.core.valators.MinValueValidator object&gt;, &lt;django.core.validators.MaxValueValidator object&gt;])
    hcomment = CharField(allow_null=<span class="hljs-keyword">True</span>, label=<span class="hljs-string">&apos;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;&apos;</span>, max_length=<span class="hljs-number">200</span>, required=<span class="hljs-keyword">False</span>)
    hbook = NestedSerializer(read_only=<span class="hljs-keyword">True</span>):
        id = IntegerField(label=<span class="hljs-string">&apos;ID&apos;</span>, read_only=<span class="hljs-keyword">True</span>)
        btitle = CharField(label=<span class="hljs-string">&apos;&#x540D;&#x79F0;&apos;</span>, max_length=<span class="hljs-number">20</span>)
        bpub_date = DateField(allow_null=<span class="hljs-keyword">True</span>, label=<span class="hljs-string">&apos;&#x53D1;&#x5E03;&#x65E5;&#x671F;&apos;</span>, required=<span class="hljs-keyword">False</span>)
        bread = IntegerField(label=<span class="hljs-string">&apos;&#x9605;&#x8BFB;&#x91CF;&apos;</span>, max_value=<span class="hljs-number">2147483647</span>, min_value=-<span class="hljs-number">2147483648</span>, required=<span class="hljs-keyword">False</span>)
        bcomment = IntegerField(label=<span class="hljs-string">&apos;&#x8BC4;&#x8BBA;&#x91CF;&apos;</span>, max_value=<span class="hljs-number">2147483647</span>, min_value=-<span class="hljs-number">2147483648</span>, required=<span class="hljs-keyword">False</span>)
        image = ImageField(allow_null=<span class="hljs-keyword">True</span>, label=<span class="hljs-string">&apos;&#x56FE;&#x7247;&apos;</span>, max_length=<span class="hljs-number">100</span>, required=<span class="hljs-keyword">False</span>)
</code></pre>
<p>4) &#x663E;&#x793A;&#x6307;&#x660E;&#x5B57;&#x6BB5;&#xFF0C;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HeroInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    hbook = BookInfoSerializer()

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = HeroInfo
        fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;hname&apos;</span>, <span class="hljs-string">&apos;hgender&apos;</span>, <span class="hljs-string">&apos;hcomment&apos;</span>, <span class="hljs-string">&apos;hbook&apos;</span>)
</code></pre>
<p>5)  &#x6307;&#x660E;&#x53EA;&#x8BFB;&#x5B57;&#x6BB5;</p>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<strong>read_only_fields</strong>&#x6307;&#x660E;&#x53EA;&#x8BFB;&#x5B57;&#x6BB5;&#xFF0C;&#x5373;&#x4EC5;&#x7528;&#x4E8E;&#x5E8F;&#x5217;&#x5316;&#x8F93;&#x51FA;&#x7684;&#x5B57;&#x6BB5;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;btitle&apos;</span>, <span class="hljs-string">&apos;bpub_date&apos;</span>&#xFF0C; <span class="hljs-string">&apos;bread&apos;</span>, <span class="hljs-string">&apos;bcomment&apos;</span>)
        read_only_fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;bread&apos;</span>, <span class="hljs-string">&apos;bcomment&apos;</span>)
</code></pre>
<h2 id="3--&#x6DFB;&#x52A0;&#x989D;&#x5916;&#x53C2;&#x6570;">3.  &#x6DFB;&#x52A0;&#x989D;&#x5916;&#x53C2;&#x6570;</h2>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>extra_kwargs</strong>&#x53C2;&#x6570;&#x4E3A;ModelSerializer&#x6DFB;&#x52A0;&#x6216;&#x4FEE;&#x6539;&#x539F;&#x6709;&#x7684;&#x9009;&#x9879;&#x53C2;&#x6570;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BookInfoSerializer</span><span class="hljs-params">(serializers.ModelSerializer)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x56FE;&#x4E66;&#x6570;&#x636E;&#x5E8F;&#x5217;&#x5316;&#x5668;&quot;&quot;&quot;</span>
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = BookInfo
        fields = (<span class="hljs-string">&apos;id&apos;</span>, <span class="hljs-string">&apos;btitle&apos;</span>, <span class="hljs-string">&apos;bpub_date&apos;</span>&#xFF0C; <span class="hljs-string">&apos;bread&apos;</span>, <span class="hljs-string">&apos;bcomment&apos;</span>)
        extra_kwargs = {
            <span class="hljs-string">&apos;bread&apos;</span>: {<span class="hljs-string">&apos;min_value&apos;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&apos;required&apos;</span>: <span class="hljs-keyword">True</span>}},
            <span class="hljs-string">&apos;bcomment&apos;</span>: {<span class="hljs-string">&apos;max_value&apos;</span>: <span class="hljs-number">0</span>, <span class="hljs-string">&apos;required&apos;</span>: <span class="hljs-keyword">True</span>}},
        }

<span class="hljs-comment"># BookInfoSerializer():</span>
<span class="hljs-comment">#    id = IntegerField(label=&apos;ID&apos;, read_only=True)</span>
<span class="hljs-comment">#    btitle = CharField(label=&apos;&#x540D;&#x79F0;&apos;, max_length=20)</span>
<span class="hljs-comment">#    bpub_date = DateField(allow_null=True, label=&apos;&#x53D1;&#x5E03;&#x65E5;&#x671F;&apos;, required=False)</span>
<span class="hljs-comment">#    bread = IntegerField(label=&apos;&#x9605;&#x8BFB;&#x91CF;&apos;, max_value=2147483647, min_value=0, required=True)</span>
<span class="hljs-comment">#    bcomment = IntegerField(label=&apos;&#x8BC4;&#x8BBA;&#x91CF;&apos;, max_value=2147483647, min_value=0, required=True)</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C03-Serializer/Deserializing.html" class="navigation navigation-prev " aria-label="Previous page: 反序列化使用"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C04-View/index.html" class="navigation navigation-next " aria-label="Next page: 视图"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
